//
//  RegistViewController.m
//  礼物go
//
//  Created by dllo on 16/6/2.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "RegistViewController.h"

@interface RegistViewController ()<UITextFieldDelegate>

@property(nonatomic,strong)UIButton *cancelButton;
@property(nonatomic,strong)UIImageView *phoneImageView;
@property(nonatomic,strong)UIImageView *passWordImageView;
@property(nonatomic,strong)UIButton *registButton;
@property(nonatomic,strong)UITextField *phoneTextField;
@property(nonatomic,strong)UITextField *passwordTextField;
@property(nonatomic,strong)UIImageView *confirmImageView;
@property(nonatomic,retain)UITextField *confirmText;

@end

@implementation RegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];

    
    self.cancelButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.cancelButton.frame = CGRectMake(10, 40, 15, 15);
    [self.cancelButton setImage:[UIImage imageNamed:@"btn_close_nightmode_14x14_@2x"] forState:UIControlStateNormal];
    [self.cancelButton addTarget:self action:@selector(cancelButtonAction) forControlEvents: UIControlEventTouchUpInside];
    [self.view addSubview:self.cancelButton];
    
    self.phoneImageView = [[UIImageView alloc] initWithFrame:CGRectMake(50, 120, 20, 20)];
    self.phoneImageView.image = [UIImage imageNamed:@"icon_signin_cellphone_14x16_@2x"];
    [self.view addSubview:self.phoneImageView];
    
    self.phoneTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.phoneImageView.frame) + 10, self.phoneImageView.frame.origin.y - 5, 260, 28)];
    self.phoneTextField.placeholder = @"请输入账户名";
    self.phoneTextField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [self.view addSubview:self.phoneTextField];
    self.phoneTextField.layer.borderWidth = 0.8;
    
    self.passWordImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.phoneImageView.frame.origin.x, CGRectGetMaxY(self.phoneTextField.frame) + 25, self.phoneImageView.frame.size.width, self.phoneImageView.frame.size.height)];
    self.passWordImageView.image = [UIImage imageNamed:@"icon_signin_password_14x16_@2x"];
    [self.view addSubview:self.passWordImageView];
    
    self.passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.passWordImageView.frame) + 10, self.passWordImageView.frame.origin.y - 5, self.phoneTextField.frame.size.width, self.phoneTextField.frame.size.height)];
    self.passwordTextField.placeholder = @"请输入密码";
    self.passwordTextField.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [self.view addSubview:self.passwordTextField];
    self.passwordTextField.layer.borderWidth = 0.8;
    
    self.confirmImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.phoneImageView.frame.origin.x, CGRectGetMaxY(self.passwordTextField.frame) + 25, self.phoneImageView.frame.size.width, self.phoneImageView.frame.size.height)];
    self.confirmImageView.image = [UIImage imageNamed:@"icon_signin_password_14x16_@2x"];
    [self.view addSubview:self.confirmImageView];
    
    self.confirmText = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.confirmImageView.frame) + 10, self.confirmImageView.frame.origin.y - 5, self.phoneTextField.frame.size.width, self.phoneTextField.frame.size.height)];
    self.confirmText.placeholder = @"再次输入密码";
    self.confirmText.layer.borderColor = [[UIColor lightGrayColor] CGColor];
    [self.view addSubview:self.confirmText];
    self.confirmText.layer.borderWidth = 0.8;
    
    self.registButton = [UIButton buttonWithType:UIButtonTypeCustom];
    self.registButton.frame = CGRectMake(self.confirmImageView.frame.origin.x + 5, self.confirmImageView.frame.origin.y + 60, self.confirmImageView.frame.size.width + self.confirmText.frame.size.width + 5, 30);
    self.registButton.backgroundColor = [UIColor redColor];
    [self.registButton setTitle:@"注册" forState: UIControlStateNormal];
    [self.registButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.view addSubview:self.registButton];
    [self.registButton addTarget:self action:@selector(registButtonAction) forControlEvents: UIControlEventTouchUpInside];
    self.registButton.layer.masksToBounds = YES;
    self.registButton.layer.cornerRadius = 5;
    
    
    
    
    
    
    
    
    
    // Do any additional setup after loading the view.
}


-(void)registButtonAction{
    NSMutableArray *array = [[DataFMDBHandle shareDataFMDBHandle] selectAllAccounts];
    if ([self.phoneTextField.text isEqualToString:@""] || [self.passwordTextField.text isEqualToString:@""]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请输入用户名密码" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }else if ([array containsObject:self.phoneTextField.text]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"用户名已被使用" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
        
    }else if (self.confirmText.text == self.passwordTextField.text) {
        
        [[DataFMDBHandle shareDataFMDBHandle] insertLoginWithAccount:self.phoneTextField.text Password:self.passwordTextField.text];
        
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        NSString *string = @"yes";
        [user setObject:string forKey:@"status"];
        [user setObject:self.phoneTextField.text forKey:@"userName"];
        [user synchronize];
        
        [self dismissViewControllerAnimated:YES completion:^{
            
            
        }];
    }else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请重新输入密码" preferredStyle:UIAlertControllerStyleAlert];
        [alert addAction:[UIAlertAction actionWithTitle:@"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            
        }]];
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }

}


-(void)cancelButtonAction{
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
    
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
   [textField resignFirstResponder];
    return YES;
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
